//
//  PennyworthNotifier.m
//  Shion
//
//  Created by Chris Karr on 8/25/08.
//  Copyright 2008 Northwestern University. All rights reserved.
//

#import "PennyworthNotifier.h"
#import "Device.h"

#define COCOA_OBSERVATION @"Pennyworth: Cocoa Observation"
#define COCOA_SENSOR @"Pennyworth: Cocoa Sensor"
#define COCOA_OBSERVATION_VALUE @"Pennyworth: Cocoa Observation Value"
#define COCOA_OBSERVATION_DURATION @"Pennyworth: Cocoa Observation Duration"

@implementation PennyworthNotifier

@synthesize timer;

- (void) awakeFromNib
{
	[super awakeFromNib];

	self.timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(refresh:) userInfo:nil repeats:YES];
}

- (void) update: (NSNotification *) msg
{
	if (msg != nil)
		[super update:msg];
	
	for (Device * d in [devices arrangedObjects])
	{
		NSMutableDictionary * note = [NSMutableDictionary dictionary];
		
		[note setValue:[NSString stringWithFormat:@"Shion: %@ Status", d.name] forKey:COCOA_SENSOR];
		[note setValue:d.actualStatus.lightLevel forKey:COCOA_OBSERVATION_VALUE];
		[note setValue:[NSNumber numberWithInteger:15] forKey:COCOA_OBSERVATION_DURATION];
		
		[[NSDistributedNotificationCenter defaultCenter] postNotificationName:COCOA_OBSERVATION object:nil];
		[[NSDistributedNotificationCenter defaultCenter] postNotificationName:COCOA_OBSERVATION object:@"Shion" userInfo:note];
	}
}

- (void) refresh:(NSTimer *) theTimer
{
	[self update:nil];
}

@end
